#include "AlphaBeta.h"

AlphaBeta::AlphaBeta() {

	nodesExpanded = 0;

}

int AlphaBeta::alphabetaMinimax(state_t node, int alpha, int beta, bool color)
{

	nodesExpanded++;

	int i = 0, actual = 0;
	bool moved = false;

    if(node.terminal())
        return node.value();

    if(color)
    {

        for(; i < 36; i++)
        {
        	if(node.outflank(true,i))
        	{
        		moved = true;
                if((actual = alphabetaMinimax(node.move(true,i), alpha, beta, false)) < beta)
                    beta = actual;
        	}

            if (beta <= alpha)
                return beta;

        }


        if(!moved)
            if((actual = alphabetaMinimax(node.move(true,i), alpha, beta, false)) < beta)
                beta = actual;

        return beta;

    }
    else
    {

        for(; i < 36; i++)
        {
        	if(node.outflank(false,i))
        	{
        		moved = true;
                if((actual = alphabetaMinimax(node.move(false,i), alpha, beta, true)) > alpha)
                    alpha = actual;

        	}

            if (beta <= alpha)
                return alpha;
        }

        if(!moved)
            if((actual = alphabetaMinimax(node.move(false,i), alpha, beta, true)) > alpha)
                alpha = actual;

	    return alpha;

    }

}

AlphaBeta::~AlphaBeta() {
}
